package com.liuchuanzheng.lcz_wanandroid.module.user

import android.annotation.SuppressLint
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.liuchuanzheng.lcz_wanandroid.base.Constant
import com.liuchuanzheng.lcz_wanandroid.module.user.bean.UserInfo

object UserManager {

    /**
     * 清除用户信息
     */
    fun cleanUserInfo() {
        SPUtils.getInstance(Constant.SP.UserInfo.spName).clear()
    }

    /**
     * 更新用户信息
     * 最好是先调用getUserInfo方法，获得UserInfo，再选择性的改变一个属性
     */
    @SuppressLint("RestrictedApi")
    fun updateUserInfo(userInfo: UserInfo?) {
        if (userInfo == null) {
            return
        }
        SPUtils.getInstance(Constant.SP.UserInfo.spName).put(Constant.SP.UserInfo.Key.data, GsonUtils.toJson(userInfo))

    }

    /**
     * 获取用户信息
     */
    fun getUserInfo(): UserInfo? {
        var userInfo: UserInfo? = null
        try {
            val json: String = SPUtils.getInstance(Constant.SP.UserInfo.spName).getString(Constant.SP.UserInfo.Key.data)
            userInfo = GsonUtils.fromJson<UserInfo>(json, UserInfo::class.java)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return userInfo
    }
    fun isLogin():Boolean{
        return getUserInfo() != null
    }


}